package aaaCfg;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

//import org.hibernate.Session;
//import org.hibernate.SessionFactory;
//import org.hibernate.engine.spi.SessionFactoryImplementor;
//import org.hibernate.jdbc.Work;
//import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
//import org.springframework.orm.hibernate4.SessionFactoryUtils;

import java.util.Properties;

//import org.hibernate.Session;
//import org.hibernate.SessionFactory;











 





 


import com.attilax.Closure;
import com.attilax.core;
import com.attilax.db.DBX;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.lang.Closure2;
import com.attilax.lang.Global;
import com.attilax.lang.Trigger;
import com.attilax.net.HttpRequest;
import com.attilax.net.websitex;
import com.attilax.persistence.DBCfg;
import com.attilax.persistence.DbService_freeDbName;
import com.attilax.persistence.DbutilX;
import com.attilax.persistence.DbxImpNodb;
//import com.attilax.db.Hb4JdbcX;
//import com.attilax.lang.Closure2;
import com.attilax.persistence.Hbx;
import com.attilax.persistence.HbxX;
import com.attilax.persistence.PX;
import com.attilax.spri.SpringUtil;
import com.attilax.sql.SqlService;
import com.attilax.util.HibernateSessionFactory;
import com.attilax.util.PropX;
//import com.focustar.entity.TMbWeixinuser;
import com.focustar.util.BaseImpl;
//import com.focusx.dao.BranchManagerDao;
//import com.focusx.dao.impl.BranchManagerDaoImpl;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.binder.AnnotatedBindingBuilder;
import com.google.inject.name.Names;


/**
 * aaaCfg.IocX4sqlite
 * @author attilax
 *2016年4月7日 下午3:42:14
 */
public class IocX4sqlite {
	
	
//	public static ThreadLocal<String>  db=new ThreadLocal<String>();
	//public static ThreadLocal<String>  db=new ThreadLocal<String>();
	//public static ThreadLocal<String>  db=new ThreadLocal<String>();
 	static Injector ati;
	//public static Map  cfgMap=new HashMap ();
//	public static Map<String, Object> map = new HashMap();
	public static String cfgPath;
	{
	//	ini();
	}
	public static 	String dbcfg_file = pathx
			.webAppPath()
			+ "/db_sqlite_4vod.ini";
 
 

	public static void main(String[] args) {
		SqlService dx = IocX4sqlite.getBean(SqlService.class);
		System.out.println(core.toJsonStrO88(dx
				.execSql("select * from timer_tab limit 5 ")));
	}

	/**
	 * 
	 * 	DBCfg dbcfg= IocX4nodb.	getDbcfg().execute(null);
			dbcfg.setDbname(req.get("db").toString());
			dbcfg.setUrl(dbcfg.getUrl()
			DbutilX dx=new DbutilX(dbcfg);
	 * @author attilax 老哇的爪子
	 * @since o92 j_48_40
	 * 
	 * @param class1
	 * @return
	 */
 synchronized	public  static <t> t getBean(Class<t> class1) {
	 
		// attilax 老哇的爪子 j_48_40 o92
		if (ati == null) // ini singleon patter..
		{
			ati = Guice.createInjector(new Module() {

				@Override
				public void configure(Binder bd) {

 
					
					
					bd.bind(SqlService.class).toInstance((SqlService) getDbcfg().execute(null));
					bd.bind(Connection.class).to(ConnectionImp.class);
				 
				 
				}

		
			});
		}

		return ati.getInstance(class1);

	}
	
	
	public static Closure2<Object, Object> getDbcfg() {
		Closure2<Object, Object> closure2 = new Closure2<Object, Object>() {

			@Override
			public Object execute(Object arg0) {
				Global.dbName.set("");
				DBCfg cfg = new DBCfg();
				try {
					// propertyReader pro=new Properties();
				
					File file = new File(dbcfg_file);
					if(!file.exists())
						throw new RuntimeException(" db cfg file is not exist:"+dbcfg_file);
					FileInputStream fis = new FileInputStream(
							file);
					Properties properties = new Properties();
					properties.load(fis);
					// pro.
					String url = properties
							.getProperty("jdbc.url");
					String driver = properties
							.getProperty("jdbc.driverClassName");

					String uname = properties
							.getProperty("jdbc.username");
					String pwd = properties
							.getProperty("jdbc.password");
				 
					// "jdbc:mysql://@host/@db?zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8";

					cfg.setUrl(url);
					cfg.setUser(uname);
					cfg.setPassword(pwd);
					cfg.setDriver(driver);
					
					DbService_freeDbName dx=new DbService_freeDbName(cfg);
					return dx;
					 
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					throw new RuntimeException(
							"cant ini dbcfg", e);
				}

			}
		};
		return closure2;
	}

}
